package com.devilvan.betrice.controller;

import com.devilvan.betrice.common.BetriceResultCode;
import com.devilvan.betrice.common.BetriceResultVo;
import com.devilvan.betrice.constant.CommonConstant;
import com.devilvan.betrice.service.SysLoginService;
import com.devilvan.betrice.util.UCollection;
import com.devilvan.betrice.util.UMessage;
import com.devilvan.betrice.vo.LoginVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author Evad.Wu
 * @Description 登录功能 控制器
 * @date 2023-08-11
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/api/loginController")
@Tag(name = "登录功能 控制器")
public class LoginController {
    private final SysLoginService sysLoginService;

    @PostMapping("/sysLogin")
    public BetriceResultVo<Map<String, Object>> sysLogin(@Validated @RequestBody LoginVo loginVo) {
        BetriceResultVo<Map<String, Object>> betriceResultVo = new BetriceResultVo<>();
        Map<String, Object> ajax = UCollection.optmizeInitialCapacity(1, UCollection.LOAD_FACTOR);
        // 生成令牌
        String token = sysLoginService.login(loginVo.getUsername(), loginVo.getPassword(), loginVo.getCode(), loginVo.getUuid());
        ajax.put(CommonConstant.TOKEN, token);
        return betriceResultVo.result(BetriceResultCode.SUCCESS_200, UMessage.message("msg.info.success"), ajax);
    }
}
